package com.bang.study.day07.leetcode;

import com.bang.study.day02.FileOperation;
import com.bang.study.day07.Map;
import com.bang.study.day07.RBTreeMap;
import com.bang.study.day07.impl.AVLMap;
import com.bang.study.day07.impl.BSTMap;
import com.bang.study.day08.RBTree;
import com.bang.study.day09.HashTable;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.Scanner;

/**
 * @Auther: Bang
 * @Date: 2019/9/18 0018 17:43
 * @Description: 测试BST和AVL的性能差异
 */
public class Main {

    private static double testMap(Map<String, Integer> map, String filename){

        long startTime = System.nanoTime();

        System.out.println(filename);
        ArrayList<String> words = new ArrayList<>();
        if(readFile(filename, words)) {
            System.out.println("Total words: " + words.size());
//            Collections.sort(words);
            for (String word : words){
                if(map.contains(word))
                    map.set(word, map.get(word) + 1);
                else
                    map.add(word, 1);
            }

            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

    public static void main(String[] args) {

        // E:\study\java_foundation\parent\data-structure\src\pride-and-prejudice.txt
        String filename = "pride-and-prejudice.txt";

        BSTMap<String, Integer> bstMap = new BSTMap<>();
        double time1 = testMap(bstMap, filename);
        System.out.println("BST Map: " + time1 + " s");

        System.out.println();

        System.out.println();

        AVLMap<String, Integer> avlMap = new AVLMap<>();
        double time3 = testMap(avlMap, filename);
        System.out.println("AVL Map: " + time3 + " s");


        System.out.println();

        RBTreeMap<String, Integer> rbTree = new RBTreeMap<>();
        double time4 = testMap(rbTree, filename);
        System.out.println("RBTreeMap: " + time4 + " s");
        System.out.println();

        HashTable<String, Integer> hashTable = new HashTable<>(130000);
        double time5 = testMap(hashTable, filename);
        System.out.println("HashTable: " + time5 + " s");
    }

    private static boolean  readFile(String filename, ArrayList<String> words){
        // 文件读取
        Scanner scanner;

        try {
//            filename = "E:\\study\\java_foundation\\parent\\data-structure\\src\\" + filename;
            filename = "G:\\company\\java_foundation\\parent\\data-structure\\src\\" + filename;
            File file = new File(filename);
            if(file.exists()){
                FileInputStream fis = new FileInputStream(file);
                scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
                scanner.useLocale(Locale.ENGLISH);
            }else {
                return false;
            }
        }
        catch(IOException ioe){
            System.out.println("Cannot open " + filename);
            return false;
        }

        // 简单分词
        // 这个分词方式相对简陋, 没有考虑很多文本处理中的特殊问题
        // 在这里只做demo展示用
        if (scanner.hasNextLine()) {
            String contents = scanner.useDelimiter("\\A").next();
            int start = firstCharacterIndex(contents, 0);
            for (int i = start + 1; i <= contents.length(); ) {
                if (i == contents.length() || !Character.isLetter(contents.charAt(i))) {
                    String word = contents.substring(start, i).toLowerCase();
                    words.add(word);
                    start = firstCharacterIndex(contents, i);
                    i = start + 1;
                } else {
                    i++;
                }
            }
        }

        return true;
    }

    // 寻找字符串s中，从start的位置开始的第一个字母字符的位置
    private static int firstCharacterIndex(String s, int start){

        for( int i = start ; i < s.length() ; i ++ ) {
            if( Character.isLetter(s.charAt(i)) ) {
                return i;
            }
        }
        return s.length();
    }
}
